﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.Department
{
    /// <summary>
    /// ChangeDepartment 的摘要说明。
    /// </summary>
    public partial class ChangeDepartmentPage : Page
    {
        private string backfilepath;
        private string DeptID;
        private string displayType;
        private int ReturnPage;
        private IList<int> selectedID;

        protected override void OnInit(EventArgs e)
        {
            cmdSubmit.ServerClick += cmdSubmit_ServerClick;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            DeptID = (Request.QueryString["DeptID"] == null) ? "" : Request.QueryString["DeptID"];
            displayType = (Request.QueryString["displayType"] == null) ? "" : Request.QueryString["displayType"];
            backfilepath = (Request.QueryString["BackFilePath"] == null) ? "" : Request.QueryString["BackFilePath"];

            string str = Request.QueryString["StaffIDS"];
            if (!string.IsNullOrEmpty(str))
            {
                // Convert the string to an int array.
                string[] stringArray = str.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
                int[] intArray = Array.ConvertAll(stringArray, s => int.Parse(s));
                selectedID = new List<int>(intArray).AsReadOnly();
            }

            if (!Page.IsPostBack)
            {
                var db = new Database();
                using (SqlDataReader dr = db.RunDataReader("dbo.sproc_Ext_GetDepartment"))
                {
                    Department.DataSource = dr;
                    Department.DataTextField = "Department_Name";
                    Department.DataValueField = "Department_ID";
                    Department.DataBind();
                }
            }
            if (Request.QueryString["ReturnPage"] != null)
            {
                ReturnPage = Int32.Parse(Request.QueryString["ReturnPage"]);
            }
            else
            {
                ReturnPage = 0;
            }

            base.OnLoad(e);
        }

        private void cmdSubmit_ServerClick(object sender, EventArgs e)
        {
            var db = new Database();
            string xml = Components.Staff.GenerateStaffXML(selectedID);
            int id = Int32.Parse(Department.Items[Department.SelectedIndex].Value);
            var prams = new[]
            {
                Database.MakeInParam("@xml", SqlDbType.Xml, -1, xml),
                Database.MakeInParam("@NewDeptID", SqlDbType.Int, 4, id)
            };
            db.RunProc("dbo.sproc_StaffMove", prams);
            if (ReturnPage == 0)
            {
                Response.Redirect(backfilepath + "?DeptID=" + DeptID + "&displayType=" + displayType);
            }
            else
            {
                Response.Redirect("../Staff/ManageStaff.aspx?DisplayType=0");
            }
        }
    }
}